package com.handpay.nocardpay.baselibrary.qualificationinfo.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.BundleBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.QualificationQueryBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.presenter.QualificationPresenterImpl;
import com.handpay.nocardpay.baselibrary.qualificationinfo.view.IQualificationStatusView;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;

import java.util.HashMap;

/**
 * Created by fplei on 2017/8/17.
 * 审核状态查看（审核中，审核失败）
 * ps:资质提交对外入口统一为该页面
 */

public class QualificationStatuInfoFragment extends BaseFragment implements IQualificationStatusView{
//    public static int QualificationStatuInfoFragmentRequestCode = 123;
    private Toolbar mToolbar;
    private TextView txt_status;
    private ImageView image_state;
    private Button goupdate;
    private QualificationPresenterImpl qualificationPresenter=null;
    private LinearLayout linear_root_checking;
    private QualificationQueryBean tempMerchQueryBean;
    private boolean isNoUploadQualification=false;
    public static QualificationStatuInfoFragment newInstance() {
        Bundle args = new Bundle();
        QualificationStatuInfoFragment fragment = new QualificationStatuInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.qualification_status_title);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_qualification_status_info;
    }

    @Override
    public void initView(View view) {
        if (AccountUtils.getAccountStatus(_mActivity) == AccountUtils.AccountStatus.NOUPLOAD) {
            isNoUploadQualification=true;
            startWithPop(QualificationBaseInfoFragment.newInstance());
            return;
        }
        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);
        linear_root_checking= (LinearLayout)view.findViewById(R.id.linear_root_checking);
        image_state = (ImageView) view.findViewById(R.id.image_state);
        txt_status=(TextView)view.findViewById(R.id.txt_status);
        goupdate=(Button)view.findViewById(R.id.goupdate);
    }

    @Override
    public void initListener() {
        if(!isNoUploadQualification)
            goupdate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reWriteQualificationInfo();
                }
            });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if(!isNoUploadQualification) {
            qualificationPresenter = new QualificationPresenterImpl(_mActivity, this);
            queryQualificationInfo();
        }
    }

    //发起资质信息查询
    private void queryQualificationInfo(){
        if (AccountUtils.getAccountPhone(_mActivity) == null) return;
        HashMap<String, Object> params = new HashMap<>();
        params.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(_mActivity), 1, null));
        params.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        params.put(BaseProtocolKey.NEWBANKFLAG, "1");
        qualificationPresenter.queryQualificationInfo(params,true);
    }

    //重新上传资质信息
    private void reWriteQualificationInfo(){
        if(tempMerchQueryBean!=null){
            Bundle bundle=new Bundle();
            BundleBean bundleBean=new BundleBean();
            bundleBean.setName(tempMerchQueryBean.getName());
            bundleBean.setCertNo(tempMerchQueryBean.getCertNo());
            bundleBean.setCardNo(tempMerchQueryBean.getCardNum());
            bundleBean.setBankCode(tempMerchQueryBean.getBankCode());
            bundleBean.setBankName(tempMerchQueryBean.getBankName());
            bundleBean.setSubBankCode(tempMerchQueryBean.getSubBankCode());
            bundleBean.setSubBankName(tempMerchQueryBean.getBankOfDeposit());
            bundleBean.setFrontCCPicStatus(tempMerchQueryBean.getFrontCCPicStatus());
            bundleBean.setFrontCCPic(tempMerchQueryBean.getFrontCCPic());
            bundleBean.setBackCCPicStatus(tempMerchQueryBean.getBackCCPicStatus());
            bundleBean.setBackCCPic(tempMerchQueryBean.getBackCCPic());
            bundleBean.setHoldPicStatus(tempMerchQueryBean.getHoldPicStatus());
            bundleBean.setHoldPic(tempMerchQueryBean.getHoldPic());
            bundleBean.setShareCode(tempMerchQueryBean.getRecommender());
            HPLog.i(HPLog.LFP_TAG,"QualificationStatuInfoFragment::reWriteQualificationInfo:bundleBean="+bundleBean.toString());
            bundle.putParcelable(BaseProtocolKey.KEY_BASEINFO,bundleBean);
            startWithPop(QualificationBaseInfoFragment.newInstance(bundle));
        }
    }


    @Override
    public void queryQualificationInfoSuccess(QualificationQueryBean qualificationQueryBean) {
        HPLog.i(HPLog.LFP_TAG,"QualificationStatuInfoFragment::queryQualificationInfoSuccess:qualificationQueryBean="+qualificationQueryBean.toString());
        if(qualificationQueryBean!=null){
            AccountUtils.setAccountStatus(_mActivity, AccountUtils.convertStatus(qualificationQueryBean.getStatus()));
            AccountUtils.setAccountFailDes(_mActivity, qualificationQueryBean.getAuditInfo());
            SettingState(qualificationQueryBean);
        }
    }


    @Override
    public void queryFail(String failMessage) {
        HPLog.i(HPLog.LFP_TAG,"QualificationStatuInfoFragment::queryQualificationInfoSuccess::queryFail:failMessage="+failMessage);
        DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.dialog_title), failMessage, false);
    }

    /***
     * 设置状态
     *
     * @param merchQueryBean 资质信息
     */
    private void SettingState(QualificationQueryBean merchQueryBean) {
        tempMerchQueryBean=merchQueryBean;
            HPLog.i(HPLog.LFP_TAG,"QualificationStatuInfoFragment::SettingState:tempMerchQueryBean="+tempMerchQueryBean.toString());
            int status = CommonUtils.objectToInt(merchQueryBean.getStatus());
            if (status == -1) {
                return;
            }
            if (status == 2) {//审核中
                image_state.setBackgroundResource(R.drawable.qualification_statu_ing);
                if (!TextUtils.isEmpty(merchQueryBean.getAuditInfo())) {
                    txt_status.setText(merchQueryBean.getAuditInfo());
                }
//            goupdate.setVisibility(View.INVISIBLE);
            }else if (status == 3) {//失败
//            goupdate.setVisibility(View.VISIBLE);
                image_state.setBackgroundResource(R.drawable.qualification_checking_fail);
                txt_status.setText(merchQueryBean.getFailDescpriction());
            }
            linear_root_checking.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hideSoftInput();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(qualificationPresenter!=null){
            qualificationPresenter.onDestory();
        }
    }
}
